<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Login - TBPLAS</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="shortcut icon" href="./img/logo.png" type="image/x-icon">
  <link rel="stylesheet" href="./css/element.css">
  <link rel="stylesheet" href="./css/index.css">
  <script src="./js/vue.js"></script>
  <script src="./js/axios.js"></script>
  <script src="./js/element.js"></script>
  <script src="./router.js"></script>

</head>

<body>
  <div id="app">
    <div class="container">
      <div style="text-align: right; padding: 10px;">
        <el-button type="primary" size="mini" @click="goToDocs">homepage</el-button>
      </div>
      <div class="form-header">
        <p>Welcome back, please login to your account.</p>
      </div>

      <div class="form-content" style="padding-top: 15px;">
        <el-form ref="form" :model="form" status-icon>
          <el-form-item prop="email" label="E-mail"
            :rules="[{ required: true, message: 'Please input email', trigger: 'blur' }, { type: 'email', message: 'Invalid email', trigger: ['blur', 'change'] }]">
            <el-input v-model="form.email" placeholder="xxx@yyy.com" clearable></el-input>
          </el-form-item>

          <el-form-item prop="password" label="Password"
            :rules="[{ required: true, message: 'Please input password', trigger: 'blur' }]">
            <el-input type="password" v-model="form.password" placeholder="Your password" clearable></el-input>
          </el-form-item>

          <div style="display: flex; justify-content: space-between; margin-bottom: 10px;">
            <a href="./register.html">Register</a>
            <a href="./forgot-password.html">Forgot password?</a>
          </div>


          <div class="submit-area">
            <button class="submit-btn" @click.prevent="submitLogin" :disabled="isSubmitting">
              {{ isSubmitting ? 'Logging in...' : 'Login' }}
            </button>
          </div>
        </el-form>
      </div>
    </div>
  </div>

  <script>

    new Vue({
      el: '#app',
      data() {
        return {
          form: {
            email: '',
            password: ''
          },
          isSubmitting: false
        };
      },
      methods: {
        goToDocs() {
          window.location.href = 'https://www.tbplas.net/#';
        },
        submitLogin() {
          this.$refs.form.validate((valid) => {
            if (!valid) return;
            this.isSubmitting = true;
            axios.post(`${api}/login`, this.form)
              .then((response) => {
                if (response.data.code == 200) {
                  this.$notify({
                    title: 'success',
                    message: 'submit successfully',
                    type: 'success'
                  });
                  storage.set(loginKey, JSON.stringify(response.data.user))
                  setTimeout(() => {
                    window.location.href = './index.html';
                  }, 1000);
                } else {
                  this.$notify.error({
                    title: 'fail to submit',
                    message: response.data.msg,
                    duration: 2000
                  });
                  errorLog('Login', response?.data?.msg)
                }

                // setTimeout(() => {
                //   window.location.href = '/dashboard';
                // }, 1000);
              })
              .catch((err) => {
                this.$notify.error({
                  title: 'Login Failed',
                  message: err,
                  duration: 2000
                });
                errorLog('Login', err)
              })
              .finally(() => {
                this.isSubmitting = false;
              });
          });
        }
      }
    });
  </script>
</body>

</html>